package com.hee.erp.common;

import com.hee.erp.constant.ReturnCode;
import com.hee.erp.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.asm.Advice;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public R<String> argumentNotValidException(MethodArgumentNotValidException e){
        return R.fail(ReturnCode.RC406.getCode(),e.getBindingResult().getFieldError().getDefaultMessage());
    }



    @ExceptionHandler(BusinessException.class)
    public R<String> businessExceptionHandler(BusinessException e){
        log.error(e.getMessage());
        return R.fail(ReturnCode.RC500.getCode(),e.getMessage());
    }

    @ExceptionHandler
    public R<String> exceptionHandler(Exception e){
        e.printStackTrace();
        return R.fail(ReturnCode.RC500.getCode(),e.getMessage() == null ? "服务器异常" : e.getMessage());
    }

}
